<?php
namespace app\admin\controller;
use think\Db;
class Adminuser extends Base
{
    //欢迎页面
    public function index(){
        return $this->fetch();
    }
   //编辑
    public function editself($id=''){
        if(is_post()){
            $d = request()->post();
            $rule = [
                'password'=>'min:6',
            ];
            $msg = [];
            $r = $this->validate($d, $rule,$msg);
            if($r!==true){
                return err($r);
            }
            if(!empty($d['password'])){
                $d['password'] = com_password($d['password']);
            }else{
                unset($d['password']);
            }
            if(model('AdminUser')->renew($d,false)){
                session('js.msg','修改成功');
                session('js.function','f5');
                return suc();
            }else{
                return err(model('AdminUser')->getError());
            }
        }
        return $this->fetch('', ['info'=>$this->userinfo]);
    }
    //列表
    public function lists(){
        if(is_post()){
            $d = request()->post();
            $map = [];
            if($d['keyword']!=''){
                $map[] = ['username','like',"%{$d['keyword']}%"];
            }
        	$rs = model('AdminUser')::where($map)
                                ->with(["adminGroup"])
                                ->order('id desc')
                                ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }
        return $this->fetch();
    }

    //添加
    public function add(){
        if(is_post()){
            $d = request()->post();
            $rule = [
                'password'=>'min:6',
            ];
            $msg = [];
            $r = $this->validate($d, $rule,$msg);
            if($r!==true){
                return err($r);
            }
            if(!empty($d['password'])){
                $d['password'] = com_password($d['password']);
            }else{
                $d['password'] = com_password('123456');
            }

            $rs = model('AdminUser')->renew($d);
            if($rs){
                if(isset($d['admin_group_id'])){
                    $rs->adminGroup()->saveAll($d['admin_group_id']);
                }
                session('js.function','preload');
                return suc();
            }else{
                return err(model('AdminUser')->getError());
            }
        }
        $group = model('AdminGroup')->column("title","id");
        return $this->fetch('', ['group'=>$group]);
    }
    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            $rule = [
                'password'=>'min:6',
            ];
            $msg = [];
            $r = $this->validate($d, $rule,$msg);
            if($r!==true){
                return err($r);
            }
            if(!empty($d['password'])){
                $d['password'] = com_password($d['password']);
            }else{
                unset($d['password']);
            }

            $rs = model('AdminUser')->renew($d,false);
            if($rs){
                if(isset($d['admin_group_id'])){
                    $rs->adminGroup()->detach();
                    $rs->adminGroup()->saveAll($d['admin_group_id']);
                }
                session('js.function','preload');
                return suc();
            }else{
                return err(model('AdminUser')->getError());
            }
        }
        $info = model('AdminUser')->get($id);
        $group = model('AdminGroup')->column("title","id");
        $checked = Db::name('admin_group_user')->where("admin_user_id",$id)->column('admin_group_id','id');
        return $this->fetch('',['info'=>$info,'group'=>$group,'checked'=>$checked]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = input();
            $rs = model('AdminUser')->del($d['id']);
            session('js.function','reload');
            if($rs){
                return suc();
            }else{
                return err(model('AdminUser')->getError());
            }
        }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
